package com.tencent.cloud.iov.common.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.tencent.cloud.iov.common.constant.CommonIntentConst;
import com.tencent.cloud.uikit.ui.BaseIovActivity;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

/**
 * 内置浏览器
 *
 * @author showxiao
 * @date 2018/10/24
 */
public class InnerBrowserActivity extends BaseIovActivity {

    private IovToolbar mToolbar;
    private WebView mWebView;
    private String mUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpActionBar();
        setUpView();
        setUpExtras();
        setUpData();
    }

    private void setUpActionBar() {
        mToolbar = SystemUiUtils.showActionBar(this, null);
        mToolbar.setHomeAsUpListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setUpView() {
        mWebView = new WebView(this);

        // 设置与 Js 交互的权限
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 避免跳转外部浏览器
                view.loadUrl(url);
                return true;
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                // 设置标题
                mToolbar.setTitle(title);
            }
        });

        setContentView(mWebView);
    }

    private void setUpExtras() {
        Intent intent = getIntent();
        if (intent != null) {
            mUrl = intent.getStringExtra(CommonIntentConst.KEY_URL);
        }
    }

    private void setUpData() {
        mWebView.loadUrl(mUrl);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && handleGoBack()) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (!handleGoBack()) {
            super.onBackPressed();
        }
    }

    /**
     * 销毁 WebView
     */
    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.clearHistory();

            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    /**
     * 点击返回上一页面，不直接退出浏览器
     *
     * @return
     */
    private boolean handleGoBack() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return false;
    }
}
